/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.conversores;

import br.com.saber.certificacao.ejbs.stateless.interfaces.ProfessorVersaoBeanLocal;
import br.com.saber.certificacao.modelos.relacionamentos.idcompostos.IdProfessorVersaoPk;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Guilherme
 */
@FacesConverter(value = "professorVersaoConverter")
public class ProfessorVersaoConverter  implements Converter{
    ProfessorVersaoBeanLocal professorVersaoBean = lookupProfessorVersaoBeanLocal();

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if (StringUtils.isBlank(string)) {
            return string;
        }
        return lookupProfessorVersaoBeanLocal().buscarProfessorVersaoPorId(new IdProfessorVersaoPk(string));
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
         if (o == null) {
            return null;
         }
         return o.toString();
    }

    private ProfessorVersaoBeanLocal lookupProfessorVersaoBeanLocal() {
        try {
            Context c = new InitialContext();
            return (ProfessorVersaoBeanLocal) c.lookup("java:global/Saber/saber-ejb/ProfessorVersaoBean!br.com.saber.certificacao.ejbs.stateless.interfaces.ProfessorVersaoBeanLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
      
      
}
